package cn.alex.sso.project.mapper;

import cn.alex.sso.project.entity.SysRoleClient;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Alex
 * @description 针对表【sys_role_client(角色和应用关联表)】的数据库操作Mapper
 * @createDate 2024-09-24 18:43:57
 * @Entity cn.alex.sso.project.entity.SysRoleClient
 */
public interface SysRoleClientMapper extends BaseMapper<SysRoleClient> {

    /**
     * 根据角色ID获取应用ID列表
     *
     * @param roleIdList 角色ID集合
     * @return 应用ID列表
     */
    default List<Long> getClientIdListByRoleId(List<Long> roleIdList) {
        if (CollUtil.isEmpty(roleIdList)) {
            return ListUtil.empty();
        }
        List<SysRoleClient> roleClientList = this.selectList(new LambdaQueryWrapper<SysRoleClient>()
                .in(SysRoleClient::getRoleId, roleIdList)
        );
        return roleClientList.stream().map(SysRoleClient::getClientId).distinct().collect(Collectors.toList());
    }

    /**
     * 根据应用ID获取角色ID列表
     *
     * @param clientIdList 应用ID集合
     * @return 角色ID列表
     */
    default List<Long> getRoleIdListByClientId(List<Long> clientIdList) {
        if (CollUtil.isEmpty(clientIdList)) {
            return ListUtil.empty();
        }
        List<SysRoleClient> roleClientList = this.selectList(new LambdaQueryWrapper<SysRoleClient>()
                .in(SysRoleClient::getClientId, clientIdList)
        );
        return roleClientList.stream().map(SysRoleClient::getRoleId).distinct().collect(Collectors.toList());
    }


}




